package com.ctl.utils.swing;

/**
 * <p>Title: Test3</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 * Jframe根据点画线段
 *
 * @author ctl
 * @version 1.1
 * @date 2022-12-23 13:46
 */

import com.alibaba.fastjson.JSONArray;
import net.sf.json.JSONObject;

import javax.swing.*;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

// 新建MyPanel类并继承JPanel
public class MyPointPanel extends JPanel {
    // 创建按钮，并初始化
    JButton jbStart = null;

    // 构造方法
    public MyPointPanel() {
        // 调用方法init()
        init();
        // 设置窗体大小和位置
        this.setLocation(0, 0);
        this.setSize(5000, 5000);
    }

    // 无参构造
    private void init() {
        String str = "3481.52,3476.32;3481.52,5168.75\n" +
                "3673.01,5168.75;3673.01,4101.45\n" +
                "3871,4101.45;3871,5357.26\n" +
                "4034.1,5357.26;4034.1,4180.64\n" +
                "4259.89,4180.64;4259.89,5768.94\n" +
                "4450.39,5768.94;4450.39,4167.07\n" +
                "4635.47,4167.07;4635.47,5755.36\n" +
                "4825.97,5755.36;4825.97,4026.79";
        JTextField jT_timer = new JTextField("50");
        JTextField jT_pointNum = new JTextField("100");
        JTextField jT_scale = new JTextField("10");
        JTextField jT_data = new JTextField(str.replaceAll("\n", "#"));
        JTextField jT_json = new JTextField();
        jT_timer.setVisible(true);
        jT_timer.setBounds(0, 0, 30, 20);
        jT_timer.setToolTipText("刷新时间间隔毫秒");
        jT_pointNum.setVisible(true);
        jT_pointNum.setBounds(30, 0, 40, 20);
        jT_pointNum.setToolTipText("总点数");
        jT_scale.setVisible(true);
        jT_scale.setBounds(70, 0, 30, 20);
        jT_scale.setToolTipText("点位缩小比例");
        jT_data.setVisible(true);
        jT_data.setBounds(250, 0, 300, 20);
        jT_data.setToolTipText("原始线段数据,多个线段使用#,单个线段使用;xy使用,间隔");
        jT_json.setVisible(true);
        jT_json.setBounds(550, 0, 600, 20);
        jT_json.setToolTipText("生成点位json数据");
        add(jT_timer);
        add(jT_pointNum);
        add(jT_scale);
        add(jT_data);
        add(jT_json);
        // 设置按钮文件
        jbStart = new JButton("点击开始");
        // 设置网格布局管理器（取消啦）
        this.setLayout(null);
        // 设置按钮的位置和大小（绝对定位）
        jbStart.setBounds(100, 0, 150, 20);
        // 添加指定的动作侦听器，以接收发自此按钮的动作事件。当用户在此按钮上按下或释放鼠标时，发生动作事件。
        // 匿名内部类
        jbStart.addActionListener(new ActionListener() {
            // 构造方法(有参)
            public void actionPerformed(ActionEvent e) {
                if (!"点击开始".equals(jbStart.getText())) {
                    return;
                }
                int timer_time = 50;
                int timer_pointNum = 100;
                int timer_scale = 10;
                try {
                    timer_time = Integer.parseInt(jT_timer.getText());
                } catch (Exception ex) {
                }
                try {
                    timer_pointNum = Integer.parseInt(jT_pointNum.getText());
                } catch (Exception ex) {
                }
                try {
                    timer_scale = Integer.parseInt(jT_scale.getText());
                } catch (Exception ex) {
                }
                System.out.println("fresh=" + timer_time + ",pointNum=" + timer_pointNum + ",scale=" + timer_scale);
                try {
                    points = PointUtil.getPointsJsonArray2(timer_pointNum, jT_data.getText());
                } catch (Exception ex) {
                    points = PointUtil.getPointsJsonArray2(timer_pointNum, null);
                }
                String jsonString = JSONArray.toJSONString(points);
                if (!jsonString.equals(jT_json.getText())) {
                    jT_json.setText(jsonString);
                }
                pointNum = points.size();
                // 创建一个时间对象
                Timer timer = new Timer();
                // 设置定时器的任务
                int finalTimer_scale = timer_scale;
                timer.schedule(new TimerTask() {
                    // 无参构造
                    @Override
                    public void run() {
                        // 调用方法
                        paint(MyPointPanel.this.getGraphics());
                        //画法1
//                        MyPointPanel.this.myPaint(autoNum++);
//                        if(autoNum.intValue()==pointNum){
//                            timer.cancel();
//                        }
                        //画法2
//                        Points bean = PointUtil.getPoints(points, 10);
//                        MyPointPanel.this.drawPolyline(bean.getX(), bean.getY(), bean.getLength());
                        //画法3
                        Points bean = PointUtil.getPoints(points.subList(0, ++autoNum), finalTimer_scale);
                        jbStart.setText("点数" + autoNum + "(x,y)".replaceAll("x", String.valueOf(bean.getX()[bean.getLength() - 1])).replaceAll("y", String.valueOf(bean.getY()[bean.getLength() - 1])));
                        MyPointPanel.this.drawPolyline(bean.getX(), bean.getY(), bean.getLength());
                        if (autoNum.intValue() == pointNum) {
                            timer.cancel();
                            autoNum = 0;
                            jbStart.setText("点击开始");
                        }
                    }
                }, 0, timer_time);// 100是0.1秒一到一次，越小动的越快
                // 设置按钮为不可视
                jbStart.setVisible(true);
            }
        });
        add(jbStart);
    }

    // 画图
    private void myPaint() {
        List<Point> points = getPoint();
        for (int i = 0; i < points.size() - 1; i++) {
            // 画线
            this.getGraphics().drawLine((int) points.get(i).getX(), (int) points.get(i).getY(), (int) points.get(i + 1).getX(), (int) points.get(i + 1).getY());
        }
    }

    private void myPaint(long autoNum) {
        List<Point> points = getPoint();
//        for (int i = 0; i < points.size() - 1; i++) {
        int index = Long.valueOf(autoNum % (pointNum - 1L)).intValue();
        // 画线
        for (int i = 0; i < index; i++) {
            this.getGraphics().drawLine((int) points.get(i).getX(), (int) points.get(i).getY(), (int) points.get(i + 1).getX(), (int) points.get(i + 1).getY());
//            this.getGraphics().drawLine((int) points.get(index).getX(), (int) points.get(index).getY(), (int) points.get(index + 1).getX(),(int) points.get(index + 1).getY());
        }
//        }
    }

    private void drawPolyline(int[] x, int[] y, Integer length) {
        this.getGraphics().drawPolyline(x, y, length);
    }

    // 获取一个周期图形的个个点的坐标
    private List<Point> getPoint() {
        // 创建集合
        List<Point> list = new ArrayList<>();
        // 获取横坐标和纵坐标
        for (int i = 0; i < pointNum; i++) {
            list.add(new Point(points.get(i).getX().intValue() / 10, points.get(i).getY().intValue() / 10));
//            list.add(new Point(points.get(i).getX().intValue(),points.get(i).getY().intValue()));
        }
        return list;
    }

    static List<com.ctl.utils.swing.Point> points = PointUtil.getPointsJsonArray2(300, null);
    static Integer pointNum = points.size();
    static Integer autoNum = 0;

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setContentPane(new MyPointPanel());
        // this.setLocation(200, 200);
        // 设置窗体的大小
        // this.setSize(666, 333);
        // 设置窗体为可视化
        // 是上面两个的组合
        jFrame.setBounds(0, 0, 5000, 5000);
        jFrame.setVisible(true);
        // 设置窗体关闭方式
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
